import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter01/pages/alphaImageIgnorePointer/index.dart';
import 'package:flutter01/pages/animation/index.dart';
import 'package:flutter01/pages/animation2/index.dart';
import 'package:flutter01/pages/automaticKeepAlive/index.dart';
import 'package:flutter01/pages/bottomSheet/index.dart';
import 'package:flutter01/pages/dio/index.dart';
import 'package:flutter01/pages/draggable/index.dart';
import 'package:flutter01/pages/form/index.dart';
import 'package:flutter01/pages/getx/index.dart';
import 'package:flutter01/pages/gradientButton/index.dart';
import 'package:flutter01/pages/poster/index.dart';
import 'package:flutter01/pages/practice/index.dart';
import 'package:flutter01/pages/read/index.dart';
import 'package:flutter01/pages/signIn/index.dart';
import 'package:flutter01/pages/splash/index.dart';
import 'package:flutter01/pages/splash/splash_binding.dart';
import 'package:flutter01/pages/sqfLite/binding.dart';
import 'package:flutter01/pages/sqfLite/index.dart';
import 'package:flutter01/pages/tabBar/index.dart';
import 'package:flutter01/pages/training/index.dart';
import 'package:flutter01/pages/training/video_info.dart';
import 'package:flutter01/pages/travel/index.dart';
import 'package:flutter01/pages/waterfall/index.dart';
import 'package:flutter01/pages/widgetKey/index.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_ume/flutter_ume.dart'; // UME 框架
import 'package:flutter_ume_kit_console/flutter_ume_kit_console.dart'; // debugPrint 插件包
import 'package:flutter_ume_kit_device/flutter_ume_kit_device.dart'; // 设备信息插件包
import 'package:flutter_ume_kit_perf/flutter_ume_kit_perf.dart'; // 性能插件包
import 'package:flutter_ume_kit_show_code/flutter_ume_kit_show_code.dart'; // 代码查看插件包
import 'package:flutter_ume_kit_ui/flutter_ume_kit_ui.dart'; // UI 插件包
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:splash/splash.dart' as splash;

void main() {
  if (kDebugMode) {
    PluginManager.instance // 注册插件
      ..register(WidgetInfoInspector())
      ..register(WidgetDetailInspector())
      ..register(ColorSucker())
      ..register(AlignRuler())
      ..register(ColorPicker()) // 新插件
      ..register(TouchIndicator()) // 新插件
      ..register(Performance())
      ..register(ShowCode())
      ..register(MemoryInfoPage())
      ..register(CpuInfoPage())
      ..register(DeviceInfoPanel())
      ..register(Console());
    // ..register(DioInspector(dio: dio)); // 传入你的 Dio 实例
    runApp(UMEWidget(child: MyApp(), enable: true)); // 初始化
  } else {
    runApp(MyApp());
  }
}

class MyApp extends StatelessWidget {
  final _routes = [
    // GetPage(name: "/my", page: () => My(), transition: Transition.rightToLeft)
    GetPage(
      name: "/",
      page: () => Home(),
    ),
    GetPage(
      name: '/travel',
      page: () => TravelIndex(),
    ),
    GetPage(
      name: '/read',
      page: () => ReadIndex(),
    ),
    GetPage(
      name: '/practice',
      page: () => PracticeIndex(),
    ),
    GetPage(
      name: '/poster',
      page: () => PosterIndex(),
    ),
    GetPage(
      name: '/training',
      page: () => TrainingIndex(),
    ),
    GetPage(
      name: '/training/videoInfo',
      page: () => VideoInfo(),
    ),
    GetPage(
      name: '/widgetKey',
      page: () => WidgetKey(),
    ),
    GetPage(
      name: '/animationPractice',
      page: () => AnimationPractice(),
    ),
    GetPage(
      name: '/animationPractice2',
      page: () => AnimationPractice2(),
    ),
    GetPage(
      name: '/gradientButton',
      page: () => GradientButtonIndex(),
    ),
    GetPage(
      name: "/tabbar",
      page: () => TabBarIndex(),
    ),
    GetPage(
      name: '/form',
      page: () => FormIndex(),
    ),
    GetPage(
        name: "/dio",
        page: () => DioIndex(),
        transition: Transition.cupertinoDialog),
    GetPage(
      name: "/getx",
      page: () => GetXIndex(),
    ),
    GetPage(name: "/splash", page: () => Splash(), binding: SplashBinding()),
    GetPage(name: "/sqfLite", page: () => SqfLite(), binding: SqfLiteBinding()),
    GetPage(
      name: "/waterfall",
      page: () => WaterfallIndex(),
    ),
    GetPage(name: "/bottomSheet", page: () => BottomSheetIndex()),
    GetPage(name: '/signIn', page: () => SignIn()),
    GetPage(name: '/automaticKeepAlive', page: () => AutomaticKeepAliveIndex()),
    GetPage(name: '/draggable', page: () => DraggableIndex()),
    GetPage(
        name: '/alphaImageIgnorePointer',
        page: () => AlphaImageIgnorePointerIndex()),
  ];

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
        splashFactory: splash.WaveSplash.splashFactory,
      ),
      initialBinding: BindingsBuilder(() {
        Get.putAsync<SharedPreferences>(() async {
          print('全局初始化SharedPreferences');
          final prefs = await SharedPreferences.getInstance();
          await prefs.setString("rootStr", "root string");
          return prefs;
        });
        Get.putAsync<String>(() async {
          final globalString =
              await Future.delayed(Duration(seconds: 10), () => "globalString");
          return globalString;
        }, tag: "globalString");
      }),
      builder: (context, child) {
        final easyload = EasyLoading.init();
        child = easyload(context, child);
        return child;
      },

      /*
      * 用Builder模式 是为了Navigator.push 能拿到 MaterialApp 的context 不然无效
      * */
      // home: Home(),

      //路由表定义
      getPages: _routes,
      defaultTransition: Transition.cupertino,
    );
  }
}

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('首页')),
        body: GridView(
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3, mainAxisSpacing: 10, crossAxisSpacing: 10),
          padding: EdgeInsets.all(10),
          children: [
            _pageCatalog(
                context, '旅游', '/travel', Colors.lightBlue, Colors.blue),
            _pageCatalog(
                context, '阅读', '/read', Colors.lightGreen, Colors.green),
            _pageCatalog(
                context, '海报', '/poster', Colors.orangeAccent, Colors.orange),
            _pageCatalog(
                context, '练习', '/practice', Colors.amber, Colors.amberAccent),
            _pageCatalog(context, '训练', '/training', Colors.deepPurpleAccent,
                Colors.deepPurple),
            _pageCatalog(context, "widgetKey", "/widgetKey", Colors.cyan,
                Colors.cyanAccent),
            _pageCatalog(context, '动画练习', "/animationPractice",
                Colors.pinkAccent, Colors.pink),
            _pageCatalog(context, "动画练习2", "/animationPractice2",
                Colors.pinkAccent, Colors.pink),
            _pageCatalog(context, "渐变按钮", "/gradientButton", Colors.greenAccent,
                Colors.green),
            _pageCatalog(context, "tabbar", "/tabbar", Colors.yellow,
                Colors.yellowAccent),
            _pageCatalog(context, "form表单", "/form", Colors.lightBlueAccent,
                Colors.lightBlue),
            _pageCatalog(
                context, "signIn", "/signIn", Colors.blue, Colors.lightBlue),
            _pageCatalog(
                context, "dio", "/dio", Colors.tealAccent, Colors.teal),
            _pageCatalog(
                context, "getX", "/getx", Colors.blue, Colors.blueAccent),
            _pageCatalog(context, "splash", "/splash", Colors.orangeAccent,
                Colors.orange),
            _pageCatalog(context, "sqfLite", "/sqfLite", Colors.green,
                Colors.greenAccent),
            _pageCatalog(context, "瀑布流", "/waterfall", Colors.lightBlue,
                Colors.blueAccent),
            _pageCatalog(context, "bottomSheet", "/bottomSheet", Colors.orange,
                Colors.orangeAccent),
            _pageCatalog(context, "AutomaticKeepAlive", "/automaticKeepAlive",
                Colors.green, Colors.greenAccent),
            _pageCatalog(
                context, '拖动', "/draggable", Colors.lightBlue, Colors.blue),
            _pageCatalog(context, '图片透明部分事件透传', '/alphaImageIgnorePointer',
                Colors.deepPurpleAccent, Colors.deepPurple)
          ],
        ));
  }

  Widget _pageCatalog(BuildContext context, String title, String routePath,
      Color color, Color borderColor) {
    return Container(
      decoration: BoxDecoration(
          border: Border.all(color: borderColor),
          color: color,
          borderRadius: BorderRadius.all(Radius.circular(6.0))),
      child: SizedBox.expand(
        child: TextButton(
          style: TextButton.styleFrom(
            splashFactory: Theme.of(context).splashFactory,
          ),
          child: Text(title, style: TextStyle(color: Colors.white)),
          onPressed: () {
            // Navigator.pushNamed(context, routePath);
            Get.toNamed(routePath);
          },
        ),
      ),
    );
  }
}
